package pakete2;


import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.sql.*;
import java.util.Vector;

import javax.swing.*;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.awt.Point;

public class EI_Ig extends JDialog {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JButton jButton = null;

	private JButton jButton1 = null;

	private KudeatzaileInterfazea kud = null;

	private JTextArea jTextArea = null;

	private JLabel jLabel = null;

	private JScrollPane jScrollPane;

	private JComboBox jComboBox = null;

	private Calendar data;

	private EI_SegurtasunArduraduna aita;

	/**
	 * Metodo honek jButton hasieratzen du
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getjButton() {
		if (jButton == null) {
			jButton = new JButton("Irakasle gatazkatsuena lortu");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					data = new GregorianCalendar();
					Vector<Integer> irakasleIDak = null;
					Vector<Integer> sarbEskIDak = null;
					Integer irak2urte = null;
					Emaitza3 ema = new Emaitza3();
					boolean sartuta;
					int irakasleId;
					Integer sarbideEsk1;
					try {
						try {
							kud = (KudeatzaileInterfazea) Naming
									.lookup("rmi://localhost/kudeatzailea");
							irakasleIDak = kud.getIrakasleak();
							for (Integer idak : irakasleIDak) {
								sartuta = false;
								irak2urte = kud.getTxartela2Urte((Integer) idak
										.intValue());

								// arazoak ekiditeko
								if (irak2urte == null)
									continue;

								// irakaslearen sarbide eskaera ukatuak lortzeko
								// emandako dataren arabera
								sarbEskIDak = kud.getSarbideEskaeraUkatuak(
										irak2urte.intValue(),
										(Integer) jComboBox.getSelectedItem());
								jTextArea.setText("Irakaslea\t kopurua:");
								if (sarbEskIDak == null)
									continue;
								if (ema.hutsaDa()) {
									ema.sartuElementua(sarbEskIDak, 0,
											irak2urte.intValue());
									sartuta = true;
								}// if
								else {
									int i = 0;
									for (Vector<Integer> emaitzaElementua : ema
											.getEmaitza()) {
										if (sarbEskIDak.size() >= emaitzaElementua
												.size()) {
											ema.sartuElementua(sarbEskIDak, i,
													irak2urte.intValue());
											sartuta = true;
											break;
										}// if
										i++;
									}// for emaitza

									if (!sartuta)
										ema.sartuElementua(sarbEskIDak, i,
												irak2urte);
								}// else
							}// for

							// bektoreko lehen 10 posizioetako sarbide eskaeren
							// jabeen
							// id-ak idatzi

							jButton1.setEnabled(true);
							// jTextArea.setText("");
							int kont = 10;
							for (Vector<Integer> bek1 : ema.getEmaitza()) {

								if (bek1.isEmpty())
									continue;
								else
									kont--;
								// lehenengo sarbide eskaera hartu jabea zein
								// den aurkitzeko
								sarbideEsk1 = bek1.firstElement();
								try {
									irakasleId = kud
											.getErabiltzaileId(sarbideEsk1
													.intValue());
									try {
										jTextArea
												.setText(jTextArea.getText()
														+ "-\n-"
														+ irakasleId
														+ "-"
														+ kud.getErabiltzailea(irakasleId)
														+ "-\t" + bek1.size());
									} catch (IllegalStateException e1) {
										// TODO Auto-generated catch block
										e1.printStackTrace();
									} catch (SQLException e1) {
										// TODO Auto-generated catch block
										e1.printStackTrace();
									}
								} catch (IllegalStateException e2) {
									// TODO Auto-generated catch block
									irakasleId = -1;
									e2.printStackTrace();
								} catch (SQLException e2) {
									// TODO Auto-generated catch block
									e2.printStackTrace();
									irakasleId = -1;
								} catch (RemoteException e3) {
									e3.printStackTrace();
								}

								if (kont == 0)
									break;
							}
						} catch (RemoteException e5) {
							System.out
									.println("RemoteExceptio errorea: EI_IG klasean");
							e5.printStackTrace();
						} catch (MalformedURLException e6) {
							e6.printStackTrace();
						} catch (NotBoundException e7) {
							e7.printStackTrace();
						}
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (ClassNotFoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}

			});
		}
		return jButton;
	}

	/**
	 * Metodo honek jButton1 hasieratzen du
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getjButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton("        Intzidentzia Sortu          ");
			jButton1.setEnabled(false);
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					KudeatzaileInterfazea kud;
					try {
						kud = (KudeatzaileInterfazea) Naming
								.lookup("rmi://localhost/kudeatzailea");

						String aukera = (String) jTextArea.getText();
						String[] zatiak = aukera.split("-");
						int kodea = Integer.parseInt(zatiak[2]);
						int txartelKod;
						String izena = zatiak[3];

						txartelKod = -1;
						try {
							// Txartelan kodea lortu
							txartelKod = kud.getTxartela(kodea);
						} catch (IllegalStateException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						try {
							kud.sortuIntzidentzia(izena, txartelKod,
									"irakasle gatazkatsuena");
							aita.eguneratu();
						} catch (IllegalStateException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					} catch (MalformedURLException e2) {
						// TODO Auto-generated catch block
						e2.printStackTrace();
					} catch (RemoteException e2) {
						// TODO Auto-generated catch block
						e2.printStackTrace();
					} catch (NotBoundException e2) {
						// TODO Auto-generated catch block
						e2.printStackTrace();
					}
					jButton1.setEnabled(false);
					EI_Ig.this.setVisible(false);
				}

			});
		}
		return jButton1;
	}

	// jComboBox
	// edozein hilabete sartu ezin ahal izateko
	private JComboBox getJComboBox() {
		Vector<Integer> aukerak = new Vector<Integer>();
		if (jComboBox == null) {
			for (int i = 1; i <= 100; i++)
				aukerak.addElement(new Integer(i));
			jComboBox = new JComboBox(aukerak);
		}
		return jComboBox;
	}

	private JTextArea getJTextArea() {
		if (jTextArea == null) {
			jTextArea = new JTextArea();
			jTextArea.setEditable(false);
			jTextArea.setAutoscrolls(true);
		}
		return jTextArea;
	}

	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane(jTextArea);
			jScrollPane.setBounds(new Rectangle(1, 94, 288, 77));
			jScrollPane.setViewportView(getJTextArea());
			jScrollPane.setToolTipText("");

		}
		return jScrollPane;
	}

	/**
	 * This is the default constructor
	 */
	public EI_Ig(EI_SegurtasunArduraduna aita) {

		super(aita, true);
		this.aita = aita;

		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 300);
		this.setLocation(new Point(400, 250));
		this.setContentPane(getJContentPane());
		this.setTitle("Irakasle Gatazkatsuena");
		try {
			Box box = Box.createHorizontalBox();
			box.add(jLabel);
			box.add(this.getJComboBox());

			Box box1 = Box.createVerticalBox();
			box1.add(this.getjButton());

			Box box2 = Box.createVerticalBox();
			box2.add(box1);
			box2.add(this.getjButton1());

			Box box3 = Box.createVerticalBox();
			box3.add(box2);
			box3.add(this.getJScrollPane());
			this.jContentPane.add(box, BorderLayout.NORTH);
			this.jContentPane.add(box3, BorderLayout.CENTER);

		} catch (Exception e) {

		}
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel = new JLabel();
			jLabel.setText("Hilabete kopurua sartu:");
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
		}
		return jContentPane;
	}

}
